package org.jeecg.modules.api.controller.music.search;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.vo.album.AlbumDetail;
import org.jeecg.modules.api.vo.artist.ArtistDetailVo;
import org.jeecg.modules.api.vo.songlist.SonglistDetailVo;
import org.jeecg.modules.api.vo.songs.SongsDetailVo;
import org.jeecg.modules.music.service.IMusAlbumSongListService;
import org.jeecg.modules.music.service.IMusArtistService;
import org.jeecg.modules.music.service.IMusSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags="音乐系统")
@RestController
@RequestMapping("api/mus/")
public class SearchAPIController {

    @Autowired
    private IMusAlbumSongListService albumSongListService;

    @Autowired
    private IMusArtistService artistService;

    @Autowired
    private IMusSongService songService;


    @ApiOperation("API-首页搜索接口-搜索建议")
    @RequestMapping(value = "/search/suggest", method = RequestMethod.GET)
    public Result reqSerachSuggest(@RequestParam(name="keywords",required = false) String keywords){

        Map<String,Object> map = new HashMap<>();
        List<String> order = new ArrayList<>();
        order.add("songs");
        order.add("artists");
        order.add("albums");
        List<AlbumDetail> albums = albumSongListService.searchSuggestAlbums(keywords);
        List<ArtistDetailVo> artists = artistService.searchSuggestArtist(keywords);
        List<SongsDetailVo> songs = songService.searchSuggestSongs(keywords);
        map.put("albums",albums);
        map.put("artists",artists);
        map.put("songs",songs);
        map.put("order",order);
        return Result.ok(map);
    }

    @ApiOperation("API-首页搜索接口-搜索")
    @RequestMapping(value = "/cloudsearch", method = RequestMethod.GET)
    public Result reqCloudSearch(@RequestParam(name="keywords",required = true) String keywords,
                                 @RequestParam(name="type",defaultValue = "100") Integer type,
                                 @RequestParam(name="limit",defaultValue = "30") Integer limit,
                                 @RequestParam(name="offset",defaultValue = "0") Integer offset
    ){
        Map<String,Object> map = new HashMap<>();
        if (type==1|| type== null){
            List<SongsDetailVo> songs = songService.searchSongs(keywords,limit,offset);
            map.put("songCount",songs.size());
            map.put("songs",songs);
        }
        else if (type==100){
            List<ArtistDetailVo> artists = artistService.searchArtist(keywords,limit,offset);
            map.put("artistCount",artists.size());
            map.put("artists",artists);
        }
        else if (type ==10){
            List<AlbumDetail> albums = albumSongListService.searchAlbums(keywords,limit,offset);
            map.put("albumCount",albums.size());
            map.put("albums",albums);
        }
        else if (type==1000){
            List<SonglistDetailVo> playlists = albumSongListService.searchPlaylists(keywords,limit,offset);
            map.put("playlistCount",playlists.size());
            map.put("playlists",playlists);
        }
        return Result.ok(map);
    }
}
